package com.sharemarking.wa.platform.mapper;

import com.sharemarking.wa.common.entity.Address;
import com.sharemarking.wa.common.entity.Order;
import com.sharemarking.wa.platform.dto.orderManagement.OrderListDto;
import com.sharemarking.wa.platform.dto.orderManagement.WasteListDto;
import com.sharemarking.wa.platform.dto.address.AddressListDto;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Map;

/**
 * @author dxy
 */
@Repository
public interface OrderManagementMapper {

    List<OrderListDto> getList(Map map);

    Integer getListTotal(Map map);

    @Select("SELECT b.pic,a.name AS wasteName,a.weight,a.real_weight " +
            "FROM wa_order_details a LEFT JOIN wa_org_waste b ON a.waste_id = b.id WHERE a.order_id = #{orderId}")
    List<WasteListDto> getWasteList(Integer orderId);

    @Select("SELECT id AS addressId,name,mobile,is_default,concat(province,city,area,address) AS address FROM sys_address WHERE member_id = #{memberId}")
    List<AddressListDto> getChooseAddress(Integer memberId);

    @Update("UPDATE wa_order SET state = #{state},receive_org_member_id = #{receiveOrgMemberId},update_date = #{updateDate},distance=#{distance},transport_fees=#{transportFees} WHERE code = #{code}")
    void acceptOrder(Order order);

    @Select("SELECT * FROM sys_address WHERE id = #{addressId} AND member_id = #{memberId}")
    Address getAddress(@Param("addressId") Integer addressId,@Param("memberId") Integer memberId);

    @Update("UPDATE wa_order SET state = #{state}, update_date = #{updateDate},complete_time = #{completeTime} WHERE id = #{id}")
    void finishOrder(Order order);

    @Delete("DELETE FROM wa_order WHERE id = #{id}")
    void cancelOrder(Integer orderId);

    @Select("SELECT CONCAT(province,city,area,address) FROM wa_order_address a LEFT JOIN wa_order b ON a.order_id = b.parent_id WHERE b.id = #{orderId} AND a.type = 0 ")
    String getOrderStartAddress(Integer orderId);

    @Select("SELECT SUM(weight) FROM wa_order_details WHERE order_id = #{orderId}")
    Integer getOrderWasteWeight(Integer orderId);

    @Select("SELECT COUNT(*) FROM wa_order WHERE parent_id = #{parentId}")
    Integer isAccept(Integer orderId);
}
